﻿package net
{
	import commonlib.DGroupInfoItem;
	
	import config.DConfig;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import net.event.DNetworkCenterEvent;
	
	/**
	* ...
	* @author
	*/
	dynamic public class DNetworkCenter extends EventDispatcher
	{
		//发送的命令
		private const CMD_REQUEST_LOGIN:String = "RQLG";						//请求登陆
		private const CMD_REQUEST_GROUP_INFO:String = "RQGI";				//请求获取房间列表
		
		//接受的命令(响应)
		private const CMD_RESPONSE_LOGIN_RESULT:String = "RELG";				//登陆结果
		private const CMD_RESPONSE_GROUP_INFO:String = "REGI";					//房间列表
		private const CMD_RESPONSE_GROUP_INFO_END:String = "REGIEND";			//房间列表结束
		private const ON_RECV_DISABLE_ACCOUNT:String = "DSAC";					//收到封号信息
		
		//接受的命令(通知)
		private const CMD_NOTIFY_MESSAGE:String = "NTMS";						//通知信息
		private const CMD_NOTIFT_LINEFAILED:String = "NTOF" ;   			    //掉线通知         
		private const CMD_NOTIFT_GIVE_GOLD:String = "NTGL" ;   				    //登录送钱
		//private const CMD_NOTIFY_FRIEND_LIST:String = "NTFD";					//好友信息
		//private const CMD_NOTIFY_FRIEND_LIST_END:String = "NTFDEND";			//好友信息结束
		
		private var m_netptr:IDBufNetwork = null;	
		
	   
		
		public function DNetworkCenter(net_ptr:IDBufNetwork) 
		{
			this.m_netptr = net_ptr;
			this.m_netptr.addEventListener(DSocketEvent.SYNC, onDataRecv);
		}
		
		private function onDataRecv(e:DSocketEvent):void 
		{
			m_netptr.readbegin();
			var strCmd:String = this.m_netptr.readString();
			var data:Array = new Array();
			
			switch(strCmd)
			{
				case CMD_RESPONSE_LOGIN_RESULT:
				{
					
					data["code"] 			= m_netptr.readShort();		
					data["lastroomname"] 	= m_netptr.readString();	
					data["lastroomid"] 		= m_netptr.readInt();					
					data["userid"] 			= m_netptr.readInt();	
					data["gamekey"] 		= m_netptr.readString();	
					data["md5"]		 		= m_netptr.readString();
					data["isnewuser"]		= m_netptr.readByte();
					//data["gold"]		 	= m_netptr.readInt();
					//DTrace.traceex("CMD_RESPONSE_LOGIN_RESULT::::::::",data)
					this.dispatchEvent(new DNetworkCenterEvent(DNetworkCenterEvent.ON_RECV_LOGIN_RESULT, data));
					break;
				}
				case ON_RECV_DISABLE_ACCOUNT:
				{
					data["userid"] = m_netptr.readInt();
					data["status"] = m_netptr.readByte();
					if (data["status"] == 1) 
					{
						data["endtime"] = m_netptr.readString();
					}
					this.dispatchEvent(new DNetworkCenterEvent(DNetworkCenterEvent.ON_RECV_DISABLE_ACCOUNT, data));
					break;
				}
				case CMD_RESPONSE_GROUP_INFO:
				{	
					var data1:Vector.<DGroupInfoItem> = new Vector.<DGroupInfoItem>();
					var groupid:String = ""; 
					var i:int = 0;
					var name:String					= m_netptr.readString();
					while ((groupid = m_netptr.readString())!= "")
					{
						var item:DGroupInfoItem= new DGroupInfoItem();
						item.groupid 			= int(groupid);
						item.groupname 			= m_netptr.readString();
						item.gamepeilv 			= m_netptr.readInt();
						item.ip 				= m_netptr.readString();
						item.port 				= m_netptr.readInt();
						item.curronline			= m_netptr.readInt();
						item.maxonline			= m_netptr.readInt();
						item.isguildroom		= m_netptr.readInt();
						item.istourroom			= m_netptr.readInt();
						item.ishighroom       	= m_netptr.readInt();	//是否为高手场 todo cw
						item.ishuanle       	= m_netptr.readInt();	//是否为欢乐场
						item.nocheat       		= m_netptr.readInt();	//是否为防作弊
						item.at_least_gold      = m_netptr.readInt();
						item.at_most_gold       = m_netptr.readInt();
						data1[i] = item;
						i++;
					}
					//DTrace.traceex("ON_RECV_GROUP_INFO:" + name + " " + data1.length);
					m_groupinfo[name] = data1;
					break;
				}
				case CMD_RESPONSE_GROUP_INFO_END:
				{	
					this.dispatchEvent(new DNetworkCenterEvent(DNetworkCenterEvent.ON_RECV_GROUP_INFO, m_groupinfo));
					break;
				}				
				case CMD_NOTIFY_MESSAGE:
				{
					data["message"]	= m_netptr.readString();
					this.dispatchEvent(new DNetworkCenterEvent(DNetworkCenterEvent.ON_RECV_MESSAGE, data));
					break;
				}
				case CMD_NOTIFT_LINEFAILED:
				{
					this.dispatchEvent(new DNetworkCenterEvent(DNetworkCenterEvent.ON_RECONNECT, null));
					break;
				}
				//登录送钱
				case CMD_NOTIFT_GIVE_GOLD:
				{
					data["gold"] 	= m_netptr.readInt();
					//this.dispatchEvent(new DNetworkCenterEvent(DNetworkCenterEvent.ON_RECV_GIVE_GOLD, data));
					break;
				}
				default:
				{
					break;
				}
			}
		}
		
		//房间信息类
		private var m_groupinfo:Array = [];
		
		//好友类
		private var frienddata:Array = null;
		private var friendIdx:int = 0;
		private var friendLoadedFlag:Boolean = false;
		
		/* INTERFACE hall.hall.net_common.IDNetworkCenter */
		
		/**
		 * 发送登录信息－－－－－－－封测代码
		 * @param	strUsername
		 * @param	strPassword
		 */
		public function sendLoginInfo(strUsername:String, strPassword:String):void
		{
			m_netptr.writeBegin();
			m_netptr.writeString(CMD_REQUEST_LOGIN);
			m_netptr.writeString(strUsername);
			m_netptr.writeString(strPassword);
			m_netptr.writeByte(DConfig.nRegSit);		//from YY
			m_netptr.writeString(DConfig.strLoginType);
			//m_netptr.writeString(activCode);            //激活码
			m_netptr.writeEnd();
		}
		
		/**
		 * 发送取得分区信息
		 * @param	strGameName
		 */
		public function sendGetAreaInfo(strGameName:String):void
		{
			m_netptr.writeBegin();
			m_netptr.writeString(CMD_REQUEST_GROUP_INFO);
			m_netptr.writeString(strGameName);
			m_netptr.writeEnd();				
		}
		
		/**
		 * 发送防踢信息
		 * @param	nToken
		 */
		public function sendToken(nToken:int):void
		{
			m_netptr.writeBegin();
			m_netptr.writeString("ANYC");
			m_netptr.writeInt(nToken);
			m_netptr.writeEnd();				
		}		
	}
	
}
